package com.hx.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hx.demo.domain.User;
import com.hx.demo.service.IUserSeervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class SecurityUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IUserSeervice userSeervice;

    /**
     * 这个类，其实只是单单判断用户名是否存在用户
     * 时间判断用户密码是否正确的是：DaoAuthenticationProvider
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //根据用户名查询用户
        User user = userSeervice.selectByUserName(username);
        //判断用户是否存在
        if (ObjectUtil.isNull(user)){
            throw new UsernameNotFoundException("该用户不存在");
        }
        return user;
    }
}
